import { Suspense, lazy } from "react";
import {
  Navigate,
  BrowserRouter as Router,
  Routes,
  Route,
} from "react-router-dom";
import PageLoading from "biz-component/components/PageLoading";
import debug from "debug";
import type { RouteType } from "types";
import Home from "@/pages/home";
import Login from "@/pages/user/login/Login";
import ApplicationLayout from "@/layout/AppLayout";
// import BizLayout from "@/layout/BizLayout";
import LoginLayout from "@/layout/LoginLayout";

const logger = debug("app:router");

const RedirectToHome = () => {
  logger("goto /");
  return <Navigate to="/app/index" />;
};

export const routes: RouteType[] = [
  {
    key: "01",
    path: "/app/index",
    component: Home,
    title: "首页",
  },
  {
    key: "02",
    path: "/app/form",
    component: lazy(() => import("@/pages/form/BusinessInfo")),
    title: "From",
  },
  {
    key: "03",
    path: "/app/crud",
    component: lazy(() => import("@/pages/crud")),
    title: "crud",
  },
  {
    key: "04",
    path: "/app/list",
    component: lazy(() => import("@/pages/list/ListShow")),
    title: "List",
  },
  {
    key: "99",
    path: "/app/user",
    component: lazy(() => import("@/pages/user/settings")),
    title: "个人设置",
  },
];

const RouterPage = () => {
  return (
    <Router basename={window.__POWERED_BY_QIANKUN__ ? "web" : ""}>
      <Routes>
        <Route path="/" element={<RedirectToHome />} />
        <Route path="/app" element={<ApplicationLayout />}>
          {routes.map((item, index) => {
            return (
              <Route
                key={index}
                path={item.path}
                element={
                  <Suspense fallback={<PageLoading />}>
                    <item.component />
                  </Suspense>
                }
              />
            );
          })}
        </Route>
        <Route path="/sys" element={<LoginLayout />}>
          <Route path="/sys/login" element={<Login />} />
          {/* <Route path="/sys/register" element={<RegisterPage />} /> */}
        </Route>
      </Routes>
    </Router>
  );
};

export default RouterPage;
